//
//  FitmentLayout.m
//  PerfectWorld
//
//  Created by dllo on 16/8/29.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "FitmentLayout.h"

@interface FitmentLayout ()
@property (nonatomic, strong)NSMutableArray<UICollectionViewLayoutAttributes *> *attriButes;
@property (nonatomic, assign)CGFloat itemWidth;
@property (nonatomic, assign)CGFloat contentHeight;
@end
@implementation FitmentLayout

- (instancetype)init {
    self = [super init];
    if (self) {
        _attriButes = [NSMutableArray array];
        _numberOfColumns = 2;
        _contentHeight = 0.0f;
        _cellMargin = 5.0f;
    }
    return self;
}

- (CGFloat)itemWidth {
    CGFloat allWidthMargin = (_numberOfColumns - 1) * _cellMargin;
    CGFloat noMarginWidth =  CGRectGetWidth(self.collectionView.bounds) - allWidthMargin;
    return noMarginWidth / _numberOfColumns;
}
- (void)prepareLayout {
    // 清空内容高度
    self.contentHeight = 0.0f;
    // 清空layoutAttributes数组
    if (_attriButes != 0) {
        [_attriButes removeAllObjects];
    }
    // 存储每一列的高度
    NSMutableArray *columnOfHeightArray = [NSMutableArray array];
    // 高度最小列数
    NSInteger column = 0;
    for (int i = 0; i < _numberOfColumns; i++) {
        // 默认高度为间距;
        [columnOfHeightArray addObject:@(_cellMargin)];
    }
    // 遍历collection中0区的所有item
    for (int i = 0; i <[self.collectionView numberOfItemsInSection:0]; i++) {
        UICollectionViewLayoutAttributes * attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:[NSIndexPath indexPathForItem:i inSection:0]];
        // 创建系统需要的布局描述类
        NSIndexPath *index = [NSIndexPath indexPathForItem:i inSection:0];
        // 最小列的起始位置
        CGFloat x = (self.itemWidth + _cellMargin) * column;
        // 高度数组中, 最小列对应得高度
        CGFloat y = [columnOfHeightArray[column] floatValue];
        // 根据列数, 计算出的宽度
        CGFloat width = self.itemWidth;
        // 通过代理方法计算得出
        CGFloat height = [self.delegate collectionView:self.collectionView layout:self width:self.itemWidth heightForItemAtIndexPath:index];
        attributes.frame = CGRectMake(x, y, width, height);
        [_attriButes addObject:attributes];
        // 更新数组中对应最小列的高度
        columnOfHeightArray[column] = @([columnOfHeightArray[column]floatValue] + height + _cellMargin);
        for (int i = 0; i < columnOfHeightArray.count; i++) {
            // 整个内容最大高度
            CGFloat currenHeight = [columnOfHeightArray[column]floatValue];
            CGFloat height = [columnOfHeightArray[i]floatValue];
            // 求出最小列
            if (currenHeight > height) {
                column = i;
                self.contentHeight = currenHeight;
            }
        }
        self.contentHeight = MAX(self.contentHeight, [columnOfHeightArray[column]floatValue ]);
    }
}
// 返回的是, 每个item对应的布局属性
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {
    return _attriButes;
}
// 负责CollectionView的滚动范围
- (CGSize)collectionViewContentSize {
    // 最高的列, 作为滚动范围的高
    return CGSizeMake(0, _contentHeight + 100);
}

@end
